
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Collections.Generic;
using SFSdotNet.Framework.Security.BusinessObjects;

namespace SFS.SampleApp.Web.Mvc
{
    public partial class SecuritySettings
    {
        public static void PermissionsInitialization() {
            string moduleKey = "SFSSampleApp";

			var module = SFSdotNet.Framework.Security.SiteMapBuilder.AddModuleIfNotExist(moduleKey, moduleKey, "SFS.SampleApp", null);

            #region permissions
            secPermission createPermission = SFSdotNet.Framework.Security.Permissions.AddPermissionIfNotExist("c", "Create", module);
            secPermission readPermission = SFSdotNet.Framework.Security.Permissions.AddPermissionIfNotExist("r", "Read", module);
            secPermission updatePermission = SFSdotNet.Framework.Security.Permissions.AddPermissionIfNotExist("u", "Update", module);
            secPermission deletePermission = SFSdotNet.Framework.Security.Permissions.AddPermissionIfNotExist("d", "Delete", module);

			 if (SFSdotNet.Framework.Security.BR.secRolesBR.Instance.GetBy(p => p.LoweredRoleName == "sfssampleapp admin").Count == 0)
                SFSdotNet.Framework.Security.BR.secRolesBR.Instance.Create(new secRole() { LoweredRoleName="sfssampleapp admin", RoleName="SFSSampleApp Admin" });
            
            
			AddPermissionIfNotExist(module, null, readPermission);
            #endregion
			
			secBusinessObject bo = null;
			secModuleObjectPermission mop =  null;
            secRoleModuleObjectPermission rmop = null;
			#region BusinessObjects
			
            #region Address
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "Address" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="Address",EntitySetName="Addresses", Name="Address", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "Addresses";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region Customer
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "Customer" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="Customer",EntitySetName="Customers", Name="Customer", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "Customers";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region CustomerAddress
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "CustomerAddress" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="CustomerAddress",EntitySetName="CustomerAddresses", Name="CustomerAddress", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "CustomerAddresses";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region Product
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "Product" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="Product",EntitySetName="Products", Name="Product", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "Products";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region ProductCategory
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "ProductCategory" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="ProductCategory",EntitySetName="ProductCategories", Name="ProductCategory", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "ProductCategories";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region ProductDescription
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "ProductDescription" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="ProductDescription",EntitySetName="ProductDescriptions", Name="ProductDescription", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "ProductDescriptions";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region ProductModel
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "ProductModel" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="ProductModel",EntitySetName="ProductModels", Name="ProductModel", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "ProductModels";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region ProductModelProductDescription
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "ProductModelProductDescription" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="ProductModelProductDescription",EntitySetName="ProductModelProductDescriptions", Name="ProductModelProductDescription", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "ProductModelProductDescriptions";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region SalesOrderDetail
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "SalesOrderDetail" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="SalesOrderDetail",EntitySetName="SalesOrderDetails", Name="SalesOrderDetail", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "SalesOrderDetails";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #region SalesOrderHeader
            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.GetBy(p=>p.BusinessObjectKey == "SalesOrderHeader" && p.secModule.ModuleKey == module.ModuleKey ).FirstOrDefault();
            if (bo == null) {
                            bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Create(new secBusinessObject() { BusinessObjectKey="SalesOrderHeader",EntitySetName="SalesOrderHeaders", Name="SalesOrderHeader", secModule = module, GuidBusinessObject= Guid.NewGuid()  });
            }else{
				bo.EntitySetName = "SalesOrderHeaders";
				bo = SFSdotNet.Framework.Security.BR.secBusinessObjectsBR.Instance.Update(bo);
			}
            AddPermissionIfNotExist(module, bo, createPermission);
            AddPermissionIfNotExist(module, bo, readPermission);
            AddPermissionIfNotExist(module, bo, updatePermission);
            AddPermissionIfNotExist(module, bo, deletePermission);
            #endregion

            #endregion
        }
		
		public static void AddPermissionIfNotExist(secModule module, secBusinessObject businessObject, secPermission permission) {
              // se asume que el permiso ya existe
            // se asume que el m?dulo ya existe
            
            if (businessObject == null)
            {
                // m?dulo y permiso ya existen
                secPermission mp = SFSdotNet.Framework.Security.BR.secPermissionsBR.Instance.GetBy(
                p => p.PermissionKey == permission.PermissionKey
                && p.secModules.Any(x=>x.ModuleKey == module.ModuleKey)
                ).FirstOrDefault();
                if (mp == null) {

                    SFSdotNet.Framework.Security.BR.secPermissionsBR.Instance.AddRelation(permission, module);
                }

                secRoleModulePermission rmp = SFSdotNet.Framework.Security.BR.secRoleModulePermissionsBR.Instance.GetBy(
                    p => p.secRole.LoweredRoleName == "superadmin"
                    && p.secModule.ModuleKey == module.ModuleKey
                
                    && p.secPermission.PermissionKey == permission.PermissionKey).FirstOrDefault();
                if (rmp == null) {
                    rmp = new secRoleModulePermission();
					rmp.secRole = SFSdotNet.Framework.Security.BR.secRolesBR.Instance.GetBy(p => p.LoweredRoleName == "superadmin").FirstOrDefault();
                    rmp.secModule = module;
                    rmp.secPermission = permission;
					rmp.IsAllowed = true;
                    SFSdotNet.Framework.Security.BR.secRoleModulePermissionsBR.Instance.Create(rmp);
                }
				rmp = SFSdotNet.Framework.Security.BR.secRoleModulePermissionsBR.Instance.GetBy(
                    p => p.secRole.LoweredRoleName == "sfssampleapp admin"
                    && p.secModule.ModuleKey == module.ModuleKey
                
                    && p.secPermission.PermissionKey == permission.PermissionKey).FirstOrDefault();
                if (rmp == null) {
                    rmp = new secRoleModulePermission();
					rmp.secRole = SFSdotNet.Framework.Security.BR.secRolesBR.Instance.GetBy(p => p.LoweredRoleName == "sfssampleapp admin").FirstOrDefault();
                    rmp.secModule = module;
                    rmp.secPermission = permission;
					rmp.IsAllowed = true;
                    SFSdotNet.Framework.Security.BR.secRoleModulePermissionsBR.Instance.Create(rmp);
                }
            }
            else { 
				secModuleObjectPermission mop = SFSdotNet.Framework.Security.BR.secModuleObjectPermissionsBR.Instance.GetBy(
					p => p.secPermission.PermissionKey == permission.PermissionKey
					&& p.secBusinessObject.BusinessObjectKey == businessObject.BusinessObjectKey
					&& p.secModule.ModuleKey == module.ModuleKey
					).FirstOrDefault();
				if (mop == null)
				{
					mop = new secModuleObjectPermission();
					mop.secModule = module;
					mop.secBusinessObject = businessObject;
					mop.secPermission = permission;

					mop = SFSdotNet.Framework.Security.BR.secModuleObjectPermissionsBR.Instance.Create(mop);
				}

				secRoleModuleObjectPermission rmop = SFSdotNet.Framework.Security.BR.secRoleModuleObjectPermissionsBR.Instance.GetBy(
					p => p.secRole.LoweredRoleName == "superadmin"
					&& p.secModule.ModuleKey == module.ModuleKey 
					&& p.secBusinessObject.BusinessObjectKey == businessObject.BusinessObjectKey 
					&& p.secPermission.PermissionKey == permission.PermissionKey).FirstOrDefault();
				if (rmop == null)
				{
					rmop = new secRoleModuleObjectPermission();
					rmop.secBusinessObject = businessObject ;
					rmop.secModule = module;
					rmop.secRole = SFSdotNet.Framework.Security.BR.secRolesBR.Instance.GetBy(p => p.LoweredRoleName == "superadmin").FirstOrDefault();
					rmop.secPermission = permission ;
					rmop.IsAllowed = true;

					SFSdotNet.Framework.Security.BR.secRoleModuleObjectPermissionsBR.Instance.Create(rmop);

				}
				
				rmop = SFSdotNet.Framework.Security.BR.secRoleModuleObjectPermissionsBR.Instance.GetBy(
					p => p.secRole.LoweredRoleName == "sfssampleapp admin"
					&& p.secModule.ModuleKey == module.ModuleKey 
					&& p.secBusinessObject.BusinessObjectKey == businessObject.BusinessObjectKey 
					&& p.secPermission.PermissionKey == permission.PermissionKey).FirstOrDefault();
				if (rmop == null)
				{
					rmop = new secRoleModuleObjectPermission();
					rmop.secBusinessObject = businessObject ;
					rmop.secModule = module;
					rmop.secRole = SFSdotNet.Framework.Security.BR.secRolesBR.Instance.GetBy(p => p.LoweredRoleName == "sfssampleapp admin").FirstOrDefault();
					rmop.secPermission = permission ;
					rmop.IsAllowed = true;

					SFSdotNet.Framework.Security.BR.secRoleModuleObjectPermissionsBR.Instance.Create(rmop);

				}
			}

        }

    }
}
